package de.IIIfeed.MoneyPlanner;

import java.sql.Timestamp;

import org.kroz.activerecord.ActiveRecordBase;
import org.kroz.activerecord.ActiveRecordException;
import org.kroz.activerecord.Database;
import org.kroz.activerecord.DatabaseBuilder;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;

public class MoneyPlanner extends TabActivity {
	
	protected static ActiveRecordBase db;
	
	//the item currently being edited by AddAndUpdate
	protected static Item item;
	
	//some constants
	public static final int ONCE = 1;
	public static final int PERIODICALLY = 2;
	
	public static final long DAILY = 1000 * 60 * 60 * 24;
	public static final long MONTHLY = DAILY * 31;
	public static final long WEEKLY = DAILY * 7;
	public static final long YEARLY = DAILY * 365;	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        DatabaseBuilder builder = new DatabaseBuilder("MoneyPlanner");
        builder.addClass(Item.class);
        Database.setBuilder(builder);

		try {
			db = ActiveRecordBase.open(this, "MoneyPlanner", 1);
		} catch (ActiveRecordException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Chart.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Chart",
                          res.getDrawable(R.drawable.ic_tabs_chart))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, Manage.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Manage",
                          res.getDrawable(R.drawable.ic_tabs_manage))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    }
    
    @Override
    protected void onResume(){
      super.onResume();
      
    }


    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.layout.menu, menu);
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem menuItem) {
	    // Handle item selection
	    switch (menuItem.getItemId()) {
	    case R.id.additem:
            //List items
	    	Intent intent = new Intent(this, AddAndUpdate.class);
	    	intent.putExtra("caller", ".MoneyPlanner");
	    	try {
	    		Item item = db.newEntity(Item.class);
	    		item.open();
	    		item.amount = 99.95F;
	    		item.title = "newItem";
	    		item.type = MoneyPlanner.ONCE;
	    		item.dateStart = new Timestamp(System.currentTimeMillis());
				MoneyPlanner.item = item;
		    	startActivity(intent);
			} catch (ActiveRecordException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        return true;
	    case R.id.preferences:
            //List items
	    	Intent intent3 = new Intent(this, Settings.class);
	    	startActivity(intent3);
	        return true;
	    case R.id.quit:
	        finish();
	        return true;
	    default:
	        return super.onOptionsItemSelected(menuItem);
	    }
	}
}